package com.zq.spring.bean;



import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author: zhaoqi
 * @Date: 2020/4/24 13:54
 * @Version 1.0
 */

public class MyDynamicProcy {
    public static void main(String[] args) {
        HelloImpl hello = new HelloImpl();
        MyInvocationHandle myInvocationHandle = new MyInvocationHandle(hello);

     // byte[] proxyClassFile = ProxyGenerator.generateProxyClass( proxyName, interfaces, accessFlags);

        Hello hello1 = (Hello) Proxy.newProxyInstance(HelloImpl.class.getClassLoader(), new Class[]{Hello.class}, myInvocationHandle);
        hello1.sysHello();
    }

    interface Hello{
        void sysHello();
    }

    private static class HelloImpl implements Hello{
        @Override
        public void sysHello() {
            System.out.println("hello World !");
        }
    }

    private static class MyInvocationHandle  implements InvocationHandler {
        private Object target;
        public MyInvocationHandle(Object target) {
            this.target=target;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("------invoke--- SayHello------");
            Object result = method.invoke(target,args);
            System.out.println("------destory--- SayHello------");
            return result;
        }
    }
}
